<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Gateway for the shift roles
 *
 * @category 	RedCross
 * @package		Core_Gateway
 */
class Core_Gateway_ShiftRole extends RedCross_Gateway_GatewayAbstract
{
	public function __construct()
	{
		$this->_class = 'ShiftRole';
		$this->_modelPath = 'Core_Model';
	}

	public function getAllRolesForShift($id)
	{
		$result = Doctrine_Query::create()
			->select()
			->from($this->getModelClass() . ' sr')
			->leftJoin('sr.Role r')
			->leftJoin('sr.Member m')
			->where('sr.shift_id = ?', $id)
			->fetchArray();

		return $result;
	}

	public function replaceRole($role, $newMemberId)
	{
        $role->unlink('Member', $role['Member']['id']);
        $role->link('Member', $newMemberId);
        return $role->save();
	}

    public function fetchForMemberRoleTable($shiftId)
    {
        $query = Doctrine_Query::create()
			->select()
			->from($this->getModelClass() . ' sr')
        	->leftJoin('sr.Shift s')
            ->leftJoin('s.Roles ro')
//			->leftJoin('sr.Role r')
//			->leftJoin('sr.Member m')
			->where('sr.id = ?', $shiftId);

		return $query->fetchOne();
    }

     public function fetchMemberNameAndRoleName($shiftRoleId)
     {
         $query = Doctrine_Query::create()
            ->select('m.first_name, m.last_name, r.name, sr.id')
            ->from('Core_Model_ShiftRole sr')
            ->leftJoin('sr.Member m')
            ->leftJoin('sr.Role r')
            ->where('sr.id = ?', $shiftRoleId)
            ->fetchArray();

         return $query;

     }

    public function fetchById($shiftRoleId)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from('Core_Model_ShiftRole sr')
            ->leftJoin('sr.Member m')
            ->leftJoin('sr.Role r')
            ->where('sr.id = ?', $shiftRoleId);

        return $query->fetchOne();
    }
}
